package com.gamefx.engine1.managers.soundClasses
{
	import com.gamefx.engine1.managers.SoundManagerImpl;
	
	import flash.events.Event;
	import flash.media.SoundChannel;

	public class ChannelBox
	{
		public var soundBox:SoundBox;
		public var soundChannel:SoundChannel;
		private var id:uint;
		
		public function ChannelBox(id:uint, soundBox:SoundBox, starTime:Number = 0, loops:uint = 0)
		{
			this.id = id;
			this.soundBox = soundBox;
			if (soundBox.sound.bytesLoaded > soundBox.sound.bytesTotal)
			{
				SoundManagerImpl.getInstance().stop(id);
			}
			else
			{
				soundChannel = soundBox.sound.play(starTime, loops);
				soundBox.playingCount++;
				soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			}
		}
		
		public function stop():void
		{
			if (soundChannel != null)
			{
				soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
				soundChannel.stop();
			}
			soundBox.playingCount--;
		}
		
		private function soundCompleteHandler(event:Event):void
		{
			SoundManagerImpl.getInstance().stop(id);
		}
	}
}